<template>
  <div class="select-city-container">
    <el-select class="select-city" v-model="fakeProvince" placeholder="请选择省份">
      <el-option v-for="(item, index) in cityOptions" :key="index" :label="item.lable" :value="item.value"></el-option>
    </el-select>
    <el-select class="select-city" v-model="fakeCity" placeholder="请选择城市">
      <el-option v-for="(item, index) in cityChildren" :key="index" :label="item.label" :value="item.value"></el-option>
    </el-select>
  </div>
</template>

<script>
/**
* @description: 城市选择框
* @props {type} province {String}
* @props {type} city {String}
* @return: selected {Function([province, city])}
*/
import cityOptions from '@/assets/utils/citydata.js';
export default {
  name: 'CitySelect',
  props: {
    province: {
      type: String,
      default: ''
    },
    city: {
      type: String,
    }
  },
  data: function() {
    return {
      fakeProvince: '',
      fakeCity: '',
      cityOptions,
      cityChildren: []
    };
  },
  methods: {},
  watch: {
    fakeProvince (val) {
      if (val) {
        this.cityChildren = this.cityOptions.find(item => {
          return item.label === val
        }).children
        if (this.cityChildren.findIndex(item => item.label === this.fakeCity) < 0) {
          this.fakeCity = ''
        }
      }
    },
    fakeCity (val) {
      if (val) {
        this.$emit('selected', [this.fakeProvince, this.fakeCity])
      }
    },
    city () {
      this.fakeProvince = this.province
      this.fakeCity = this.city
    }
  }
};
</script>

<style lang="less" scoped>
.select-city-container {
  display: flex;
  justify-content: space-between;
}
.select-city+.select-city {
  margin-left: 20px;
}
</style>
